package com.unitymarvel.harmonyos.projects.findme.common;

import com.unitymarvel.harmonyos.projects.findme.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;

import java.util.List;
///////////////////////////////////////////////////
// 项目的详细实现过程：https://ke.qq.com/course/3677276?tuin=a22a65ce
// 关注微信公众号和B站，获取更多鸿蒙学习资源
// 微信公众号：极客起源
// B站：https://space.bilibili.com/477001733
//
// 作者:李宁（蒙娜丽宁）
//
// 版本: 1.0
// Copyright (C), 2001-2022, 李宁
// 程序被版权法保护
//
///////////////////////////////////////////////////
public class DeviceProvider extends BaseItemProvider {
    private static final String STATUS_ONLINE = "在线";
    private static final String STATUS_OFFLINE = "离线";

    private Context context;

    private List<DeviceInfo> deviceInfoList;

    public DeviceProvider(Context context, List<DeviceInfo> deviceInfoList) {
        this.context = context;
        this.deviceInfoList = deviceInfoList;
    }

    @Override
    public int getCount() {
        return deviceInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return deviceInfoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item,null, false);
        Text textType = (Text)container.findComponentById(ResourceTable.Id_text_type);
        Text textName = (Text)container.findComponentById(ResourceTable.Id_text_name);
        Text textStatus = (Text)container.findComponentById(ResourceTable.Id_text_status);

        DeviceInfo deviceInfo = deviceInfoList.get(i);
        boolean deviceOnline = false;
        if(deviceInfo != null) {
            deviceOnline = deviceInfo.isDeviceOnline();
            if((deviceInfo.getDeviceType() != null) && (deviceInfo.getDeviceType().toString() != null)) {
                textType.setText(deviceInfo.getDeviceType().toString().substring(6));
            }
            textName.setText(deviceInfo.getDeviceName());
        }
        textStatus.setText(deviceOnline?STATUS_ONLINE:STATUS_OFFLINE);
        textType.setTextColor(deviceOnline? Color.GREEN:Color.RED);
        textName.setTextColor(deviceOnline? Color.GREEN:Color.RED);
        textStatus.setTextColor(deviceOnline? Color.GREEN:Color.RED);
        return container;
    }
    public void updateData(List<DeviceInfo> data) {
        deviceInfoList = data;
        notifyDataChanged();
    }
}
